package com.luis.toolbox.outbound;

import com.influxdb.client.InfluxDBClient;
import com.influxdb.client.InfluxDBClientFactory;
import com.influxdb.client.WriteApiBlocking;
import com.influxdb.client.domain.WritePrecision;
import com.luis.toolbox.inbound.Router;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.MessageHandler;

@Configuration
@ConditionalOnProperty(prefix="outbound",name = "destination", havingValue = "influx2")
@SuppressWarnings("Not Validated")
public class Influx2Producer {

	private final Logger log = LoggerFactory.getLogger(Influx2Producer.class);
	private final char[] token = "my-token".toCharArray();
	private final String org = "my-org";
	private final String bucket = "my-bucket";
	@Bean
	@ServiceActivator(inputChannel = "influx2OutboundChannel")
	public MessageHandler influx2Outbound() {
		return message -> {
			InfluxDBClient influxDBClient = InfluxDBClientFactory.create("http://localhost:8086", token, org, bucket);
			WriteApiBlocking writeApi = influxDBClient.getWriteApiBlocking();
			writeApi.writeRecord(WritePrecision.MS, message.getPayload().toString());
		};
	}

}
